package cn.walls1717.wallsmall.search.thread;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 实现Callable接口
 * 
 * 使用Callable可以接收返回值
 * 
 * @author walls1717
 * @since 2022/11/08 20:10
 **/
@Slf4j
public class ThreadTest3 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        log.info("main -> start");
        Thread03 thread03 = new Thread03();
        FutureTask<Integer> target = new FutureTask<>(thread03);
        new Thread(target).start();
        Integer integer = target.get();
        log.info(integer.toString());
        log.info("main -> end");
    }

    public static class Thread03 implements Callable<Integer> {
        @Override
        public Integer call() throws Exception {
            log.info("当前线程id -> {}", Thread.currentThread().getId());
            int i = 10 / 2;
            log.info("运行结果 -> {}", i);
            return i;
        }
    }
}
